<template>
	<view>
		<view class="title">
			人员选择
		</view>

		<view class="list-box" v-for="item in inner_user_list">
			<u-collapse accordion>
				<u-collapse-item :title="item.department_name">
					<view class="user-list" v-for="info in item.children" @click="selectUser(info)">
						<view class="user-info">{{info.show_name}}</view>
						<image :src="require('../static/img/icon_selcircle_yuantu.png')" v-if="info.is_select == '0'"></image>
						<image :src="require('../static/img/icon_selcircle1.png')" v-else></image>
					</view>
				</u-collapse-item>
			</u-collapse>
		</view>
		<!-- <view style="color: #008BFE;">{{item.department_name}}</view>
		<view class="user-list" v-for="info in item.children" @click="selectUser(info)">
			<view class="user-info">{{info.show_name}}</view>
			<image :src="require('../static/img/icon_selcircle_yuantu.png')" v-if="info.is_select == '0'"></image>
			<image :src="require('../static/img/icon_selcircle1.png')" v-else></image>
		</view> -->
		
	<!-- 	<view class="title">
			外部人员选择
		</view>
		
		<view class="list-box">
			<u-collapse accordion>
				<u-collapse-item title="外部人员">
					<view class="user-list" v-for="item in out_user_list" @click="selectOutUser(item)">
						<view class="user-info">{{item.nickname}}</view>
						<image :src="require('../static/img/icon_selcircle_yuantu.png')" v-if="item.is_select == '0'"></image>
						<image :src="require('../static/img/icon_selcircle1.png')" v-else></image>
					</view>
				</u-collapse-item>
			</u-collapse>
		</view> -->
		

		
		<view class="login_btn all_text_center" @click="addAdmin()">
			添加
		</view>

		<view class="" style="width: 100%;height: 200rpx;">

		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				nickname: '',

				token: '',
				member_id: '',
				value: '0',

				is_outuser: '',

				inner_user_list: [],
				out_user_list: []

			}
		},
		onLoad() {
			this.token = uni.getStorageSync('chat_token') || ''
			this.member_id = uni.getStorageSync('chat_member_id') || ''
			this.is_outuser = uni.getStorageSync('chat_is_outuser') || ''
			// this.getOutUserList()
			this.getInUserList()
		},
		methods: {
			// 选择外部人员
			selectOutUser(item){
				if (item.is_select == '0') {
					item.is_select = '1'
				} else {
					item.is_select = '0'
				}
				this.$forceUpdate()
			},
			
			// 选择内部人员
			selectUser(info) {
				if (info.is_select == '0') {
					info.is_select = '1'
				} else {
					info.is_select = '0'
				}
				this.$forceUpdate()
			},

			// 获取内部人员列表
			async getInUserList() {
				const res = await this.$myRequest({
					url: 'Management/simple_member',
					method: 'POST',
					data: {
						member_id: this.member_id,
						token: this.token,
						company_id: '1'
					},
				});
				if (res.data.code != 200) return uni.showToast({
					title: res.data.datas.msg,
					icon: 'none'
				})
				this.inner_user_list = res.data.datas.listInfo

				for (var i = 0; i < this.inner_user_list.length; i++) {
					for (var x = 0; x < this.inner_user_list[i].children.length; x++) {
						this.inner_user_list[i].children[x].is_select = '0'
					}
				}
			},

			// 获取外部人员列表
			async getOutUserList() {
				const res = await this.$myRequest({
					url: 'Chat/group_member_list',
					method: 'POST',
					data: {
						member_id: this.member_id,
						token: this.token,
						company_id: '1',
						is_outuser: this.is_outuser
					},
				});
				if (res.data.code != 200) return uni.showToast({
					title: res.data.datas.msg,
					icon: 'none'
				})
				this.out_user_list = res.data.datas.outuserlist.outuserlist
				
				for (var i = 0; i < this.out_user_list.length; i++) {
					this.out_user_list[i].is_select = '0'
				}
			},

			// 点击添加群聊
			async addAdmin() {
				// 将选择的内部人员id拼接
				let member_ids = ''
				for (var i = 0; i < this.inner_user_list.length; i++) {
					for (var x = 0; x < this.inner_user_list[i].children.length; x++) {
						if (this.inner_user_list[i].children[x].is_select == '1') {
							member_ids = this.inner_user_list[i].children[x].id + ',' + member_ids
						}
					}
				}
				
				if(member_ids == ''){
					return uni.showToast({
						title:'请选择添加人员',
						icon:"none"
					})
				}

				const res = await this.$myRequest({
					url: 'Chat/admin_member_add',
					method: 'POST',
					data: {
						member_id: this.member_id,
						token: this.token,
						member_ids: member_ids,
						is_outuser:this.is_outuser,
						company_id:'1'
					},
				});
				if (res.data.code != 200) return uni.showToast({
					title: res.data.datas.msg,
					icon: 'none'
				})

				uni.showToast({
					title: res.data.datas.msg,
					icon: 'none'
				})

				setTimeout(() => {
					uni.navigateBack({
						delta: 1
					})
				}, 1000);
			}

		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		justify-content: center;
	}

	.list-box {
		// margin-left: 30rpx;
		margin-top: 30rpx;

		.user-list {
			// margin-left: 30rpx;
			margin-top: 20rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			padding-bottom: 10rpx;
			border-bottom: 1rpx solid #999999;

			.user-info {}

			image {
				width: 40rpx;
				height: 40rpx;
			}
		}
	}

	.argeement_line {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-top: 20rpx;

		image {
			width: 26rpx;
			height: 26rpx;
		}

		.left_box {
			width: 22rpx;
			height: 22rpx;
			border-radius: 8rpx;
			border: 1rpx solid #999999;
		}

		.rigit {
			font-size: 24rpx;
			margin-left: 20rpx;

			color: #9E9E9E;
		}
	}

	.title {
		margin-top: 20rpx;
		font-size: 36rpx;
		font-weight: 500;
		color: #333333;
	}

	.input_title {
		margin-top: 50rpx;
		font-size: 32rpx;
		font-weight: 500;
		color: #333333;
	}

	.input_class {
		margin-top: 20rpx;
		width: 540rpx;
		border-bottom: 1rpx solid #D8D8D8;
		padding-bottom: 10rpx;
		padding-left: 5rpx;
	}

	.mini_text {
		font-weight: 500;
		color: #008BFE;
		font-size: 26rpx;
		display: flex;
		justify-content: flex-end;
		margin-top: 20rpx;
	}

	.login_btn {
		width: 540rpx;
		height: 80rpx;
		background: #008BFE;
		color: #ffffff;
		box-shadow: 0px 1px 4px 1px rgba(0, 0, 0, 0.4);
		border-radius: 12rpx;
		font-size: 36rpx;
		margin-top: 60rpx;
	}

	.login_btn_off {
		width: 540rpx;
		height: 80rpx;
		background: #EFEFEF;
		color: #999999;
		box-shadow: 0px 1px 4px 1px rgba(0, 0, 0, 0.4);
		border-radius: 12rpx;
		font-size: 36rpx;
		margin-top: 60rpx;
	}

	.mini_text_line {
		width: 540rpx;
		margin-top: 25rpx;
		font-weight: 500;
		color: #008BFE;
		font-size: 26rpx;
		display: flex;
		justify-content: space-between;
	}
</style>
